#!/bin/bash

echo "Running commit-msg checks on your commit message..."

# 忽略merge request
MERGE_MSG=`cat $1 | egrep '^Merge branch*'`

if [ "$MERGE_MSG" != "" ]; then
    echo -e "\033[32mmerge branch, ignored check\033[0m"
    exit 0
fi
# git commit -m 'feat(user): add info api'
COMMIT_MSG=`cat $1 | egrep "^(feat|fix|docs|style|refactor|perf|test|chore|revert|merge)(\((\w|\w,\w|\*)+\)):\s(\S|\w)+"`

if [ "$COMMIT_MSG" = "" ]; then
#printf "\033[31m COMMIT FAILED \033[0m\n"
    echo -e "\033[31mCommit message irregular, please check!\033[0m"
    exit 1
fi

if [ ${#COMMIT_MSG} -lt 10 ]; then
    echo -e "\033[31mCommit message too short, at least 10 characters!\033[0m"
    exit 1
fi

if [ ${#COMMIT_MSG} -gt 80 ]; then
    echo -e "\033[31mCommit message too long, at most 80 characters!\033[0m"
    exit 1
fi

echo -e "\033[32mCommit Message regular, passed!\033[0m"

exit 0
